package com.example.myanimator.drawpictrue;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Rect;
import android.graphics.Region;
import android.graphics.RegionIterator;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.view.View;

import androidx.annotation.Nullable;

/**
 * @author tgw
 * @date 2021/3/29
 * @describe
 */
public class DrawTextPath extends View {

    private Paint mPaint;
    private Path mPath;
    private Canvas mCanvas;
    private String tipStr = "asdfghjei汉字";

    public DrawTextPath(Context context) {
        super(context);
    }

    public DrawTextPath(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    private void init() {
        mPaint = new Paint();
        mPaint.setAntiAlias(true); // 去除锯齿
        mPaint.setDither(true); // 抗抖动
        mPaint.setStrokeWidth(2);//设置笔画宽度
        mPaint.setStyle(Paint.Style.STROKE);
//        mPaint.setTextAlign(Paint.Align.CENTER);//设置文字对齐方式
        mPaint.setTextSize(100);//设置文字大小
        //
        mPaint.setFakeBoldText(true);//是否为粗体
//        mPaint.setUnderlineText(true);//设置下划线
        mPaint.setTextSkewX((float) -0.25);//设置字体水平倾斜度，普通斜体字设为-0.25
//        mPaint.setStrikeThruText(true);//设置带有删除线效果
        mPaint.setTextScaleX(2);//水平方向字体拉伸，高度不变
        mPaint.setColor(Color.BLACK);
        mPaint.setSubpixelText(true);//打开亚像素绘制文本--会增强文本清晰度，但会牺牲计算器性能
        mPath = new Path();
        mCanvas = new Canvas();
    }


    @Override
    protected void onDraw(Canvas canvas) {
        int topDefault = 100 ;

        int baseLineX = 0;
        int baseLineY = (int) (topDefault-mPaint.getFontMetrics().top);//根据给出的 顶部坐标 计算 基线
        canvas.drawColor(Color.YELLOW);
        //画文字基线
        canvas.drawLine(baseLineX, baseLineY, getWidth(), baseLineY, mPaint);
//        mPaint.setTextAlign(Paint.Align.CENTER);//设置文字对齐方式 ,即代表 基点  处于文字何处
        canvas.drawText(tipStr, baseLineX, baseLineY, mPaint);

        /** Android 给我们提供了 FontMetrics 类，它里面有 成员变量。
         FontMetrics: :ascent;
         FontMetr cs: : descent;
         FontMetrics: :top;
         FontMetrics: :bottom;

         个成员变量的含义与值的计算方法分别如下
         • .FontMetrics.ascent = ascent 线的 y坐标 减（-） baseline 线的 y坐标
         • FontMetrics.descent = descent 线的 坐标 减（-）baseline 线的 y坐标
         • FontMetrics.top= top 线的 y 坐标  减（-） baseline 线的y 坐标
         • FontMetrics.bottom = bottom 线的 y坐标 减（-） baseline的y坐标
            //当给定中间线的坐标时 基线计算公式
         baseline= center + (FontMetrics.bottom - FontMetrics.top)/2 -FontMetrics.bottom
         */

        //计算各条线坐标
        Paint.FontMetrics fm = mPaint.getFontMetrics();
        float ascent = baseLineY + fm.ascent;
        float descent = baseLineY + fm.descent;
        float top = baseLineY + fm.top;
        float bottom = baseLineY + fm.bottom;
        mPaint.setColor(Color.RED);
        //画基线
        mPaint.setColor(Color.RED);
        canvas.drawLine(baseLineX, baseLineY, 3000, baseLineY, mPaint);
        //画 top 当前可绘制最顶线
        mPaint.setColor(Color.BLUE);
        canvas.drawLine(baseLineX, top, 3000, top, mPaint);
        //画 ascet 当前可绘制顶线
        mPaint.setColor(Color.GREEN);
        canvas.drawLine(baseLineX, ascent, 3000, ascent, mPaint);
        //画 descent 当前可绘制底线
        mPaint.setColor(Color.RED);
        canvas.drawLine(baseLineX, descent, 3000, descent, mPaint);
        //画 bottom 当前可绘制最底线
        mPaint.setColor(Color.BLACK);
        canvas.drawLine(baseLineX, bottom, 3000, bottom, mPaint);
    }
}
